package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/registered")
public class RegisteredController {

    @Autowired
    private IRegisteredService registeredService;

    @Autowired
    private IPatientsService patientsService;

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IRegistrationService registrationService;


    @RequestMapping("/registeredManager")
    public String registeredManager(Model m){

        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        List<Dept> deptName = deptService.list(wrapper);

        m.addAttribute("deptName",deptName);


        QueryWrapper<Doctor> wrapper1 = new QueryWrapper<>();

        List<Doctor> doctors = doctorService.list(wrapper1);

        m.addAttribute("doctors",doctors);


        QueryWrapper<Registration> wrapper2 = new QueryWrapper<>();

        List<Registration> registrationTypes = registrationService.list(wrapper2);

        m.addAttribute("registrationTypes",registrationTypes);


        QueryWrapper<Registration> wrapper3 = new QueryWrapper<>();

        List<Registration> registrationMoneys = registrationService.list(wrapper3);

        m.addAttribute("registrationMoneys",registrationMoneys);


        return "registeredManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize){

        //分页对象
        Page<Registered> page = new Page<>(pageNum, pageSize);
        //条件对象
        QueryWrapper<Registered> queryWrapper = new QueryWrapper<>();

        //查询结果
        Page<Registered> result = registeredService.page(page,queryWrapper);
        return new ResultObject(result);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public MessageObject insert(Patients patients) {

        patients.setUpdateTime(LocalDate.now());
        boolean flag = patientsService.saveOrUpdate(patients);

        MessageObject mo = new MessageObject(1, "新增数据成功");
        if (!flag) {
            mo = new MessageObject(0, "新增数据失败,请联系管理员");
        }
        return mo;
    }

    @RequestMapping("/insertRegistered")
    @ResponseBody
    public MessageObject insertRegistered(Registered registered) {


        boolean flag = registeredService.saveOrUpdate(registered);

        MessageObject mo = new MessageObject(1, "新增数据成功");
        if (!flag) {
            mo = new MessageObject(0, "新增数据失败,请联系管理员");
        }
        return mo;
    }

}

